﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;


namespace Utilities.Common
{
    public static class AttributeHelper
    {        
        //cuando la propiedad es de tipo clase, no devuelve bien si uso la sobrecarga de abajo
        internal static Attribute GetAttribute(object parent, MemberInfo obj, Type attributeType)
        {
            object[] attributes = parent.GetType().GetProperty(obj.Name).GetCustomAttributes(attributeType, true);
            return attributes.Length == 1 ? (Attribute)attributes[0] : null;
        }

        internal static Attribute GetAttribute(object obj, Type attributeType)
        {
            System.Reflection.MemberInfo info = obj.GetType();
            object[] attributes = info.GetCustomAttributes(attributeType, true);
            return attributes.Length == 1 ? (Attribute)attributes[0] : null;
        }

        //ver de unificar con la primer sobrecarga
        internal static Attribute GetAttribute2(Type parentType, string property, Type attributeType)
        {
            object[] attributes = parentType.GetProperty(property).GetCustomAttributes(typeof(NotSaveAttribute), true);
            return attributes.Length == 1 ? (Attribute)attributes[0] : null;
        }

    }
}
